/*
 * @Description: 电话号码的字母组合
 * @FilePath: \网易云音乐e:\CSS+HTML+JS+\学习笔记\代码测试\算法\字符串\17.电话号码的字母组合.js
 */ 
/*
 * @lc app=leetcode.cn id=17 lang=javascript
 *
 * [17] 电话号码的字母组合
 */

// @lc code=start
/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function(digits) {
    let len = digits.length;
    if(len < 1) return [];
    // 映射
    let arr = [];
    for(let i = 0; i < digits.length; i++) {
        let res = '';
        if(digits[i] == 2) { res = 'abc' }
        if(digits[i] == 3) { res = 'def' }
        if(digits[i] == 4) { res = 'ghi' }
        if(digits[i] == 5) { res = 'jkl' }
        if(digits[i] == 6) { res = 'mno' }
        if(digits[i] == 7) { res = 'pqrs' }
        if(digits[i] == 8) { res = 'tuv' }
        if(digits[i] == 9) { res = 'wxyz' }
        arr.push(res);
    }

    if(len == 1) return [...arr[0]];

    // 两个一合并
    return merge(arr);
};


function merge (arr) {
    if(arr.length <= 1) return arr[0];
    let res = [];
    for(let i = 0; i < arr[0].length; i++) {
        for(let j = 0; j < arr[1].length; j++) {
            res.push(arr[0][i]+arr[1][j]+'');
        }
    }
    arr.splice(0, 2, res);
    return merge(arr);
}


console.log(letterCombinations('233'));
// @lc code=end

